<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Controllers_Template extends Gio_Core_Controller
{
	public function listAction()
	{
		$templatesDir = TEMPLATE_DIR;
		$configs = Gio_Core_Config_Xml::getConfig('web');
		if (!file_exists($templatesDir)) {
			return;
		}
		$templatesIterator = new DirectoryIterator($templatesDir);
		$templates = array();
		foreach ($templatesIterator as $template) {
			if ($template->isDot()) {
				continue;
			}
			$templateName = $template->getFilename();
			if (('CVS' == $templateName) || ('.svn' == strtolower($templateName)) 
					|| ('index.html' == $templateName)
					|| ('.htaccess' == $templateName)
					|| ((string)$configs->admin->template == $templateName)) {
                continue;
            }
            $templates[] = $templateName;
		}
		$this->view->templates = $templates;
		$this->view->currentTemplate = (string)$configs->template;
	}
	
	public function setAction()
	{
		$request = $this->getRequest();
		
		$this->disableLayout();
		$this->setNoRender();
		$templateName = $request->getPost('template_id');
		
		if (!$request->isPost() || !$templateName) {
			$this->getResponse()->setBody('RESULT_NOT_OK');;
		}
		
		$configs = Gio_Core_Config_Xml::getConfig();
		$configs->web->template = $templateName;
		
		$content = $configs->asXML();
		$fileName = Gio_Core_Config_Xml::getConfigFile();
		Gio_Core_File::writeToFile($fileName, $content);
		$this->getResponse()->setBody('RESULT_OK');
	}
}